package com.yunhe.option.controller.base;

import com.aliyun.oss.model.ObjectMetadata;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.utils.AliOssApiUtils;
import com.yunhe.option.domain.FileBase64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName: OssFileUploadController
 * @Description:
 * @author: GongDaCheng
 * @Date: 9:28 2021/8/11
 */
@Api(value = "FileUpload", description = "文件上传", tags = {"Model_File_Upload"})
@RestController
@RequestMapping(value = "/ossFile")
public class OssFileUploadController {

    /**
     * 文件上传
     */
    @PostMapping(value = "/uploadFile")
    @ApiOperation(value = "文件上传")
    public ResponseEntity<ResultObject<String>> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        String url = AliOssApiUtils.putObjectByte(file);
        return new ResponseEntity<>(new ResultObject<>(url), HttpStatus.OK);
    }

    /**
     * 文件上传
     */
    @PostMapping(value = "/uploadBase64")
    @ApiOperation(value = "图片上传")
    public ResponseEntity<ResultObject<String>> uploadFile(@RequestBody FileBase64 fileBase64) throws Exception {
        ByteArrayInputStream inputStream = null;
        try {
            String content = fileBase64.getBase64Url().trim();
            //获取到前缀的定位
            int index = content.indexOf("base64,")+7;
            String dataStr = content.substring(0, index);
            String theFileExtension = "."+dataStr.substring(dataStr.indexOf("data:image/")+"data:image/".length(),dataStr.indexOf(";base64"));
            String newImage = content.substring(index, content.length());
            byte[] bytes = new BASE64Decoder().decodeBuffer(newImage);
            inputStream = new ByteArrayInputStream(bytes);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(inputStream.available());
            String url = AliOssApiUtils.putObjectByte(inputStream,theFileExtension,metadata);
            return new ResponseEntity<>(new ResultObject<>(url), HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }

        return  null;
    }


    /**
     * 文件删除
     */
    @PostMapping(value = "/deleteFile")
    @ApiOperation(value = "文件删除")
    public ResponseEntity<ResultObject<Void>> deleteFile(@RequestParam("url") String url) throws IOException, ArgumentErrorException {
        AliOssApiUtils.deleteObject(url);
        return new ResponseEntity<>(new ResultObject(), HttpStatus.OK);
    }
}
